package com.tt.lcd.qunyangGPRS.chanfangActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.tt.lcd.qunyangGPRS.R;
import com.tt.lcd.qunyangGPRS.dao.renshen.XiaoqunyangDao;
import com.tt.lcd.qunyangGPRS.entity.XiaoQunYnag;
import com.tt.lcd.qunyangGPRS.zxing.activity.CaptureActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AddFeeder extends Activity {
	private TextView tv_add;

	private GridView m_paragrid;// 表格用
	private int colNum; // 表格用
	private SimpleAdapter simpleAdapter;
	private List<Map<String, Object>> listItems;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chanfang_addfeeder);

		tv_add = (TextView) findViewById(R.id.rs_addfeeder_add);

		tv_add.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 打开扫描界面扫描条形码或二维码
				Intent openCameraIntent = new Intent(AddFeeder.this,
						CaptureActivity.class);
				startActivityForResult(openCameraIntent, 1);
			}
		});

		tv_add.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					// 更改为按下时的背景图片
					v.setBackgroundColor(Color.rgb(0x08, 0x70, 0xd0));
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					// 改为抬起时的图片
					v.setBackgroundColor(Color.rgb(0x08, 0x80, 0xd0));
				}
				return false;
			}
		});

		m_paragrid = (GridView) findViewById(R.id.paragrid);
		colNum = 2;
		m_paragrid.setNumColumns(2);// -------设置表格6列

		// 创建一个List对象，List对象的元素是Map
		listItems = new ArrayList<Map<String, Object>>();
		setListItems(listItems);
		// 创建一个SimpleAdapter
		simpleAdapter = new GridAdapter(this,
				listItems
				// 使用/layout/cell.xml文件作为界面布局
				, R.layout.cell, new String[] { "cell" },
				new int[] { R.id.CellView });
		// 为GridView设置Adapter
		m_paragrid.setAdapter(simpleAdapter);

		// 添加列表项被单击的监听器
		m_paragrid.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
									int position, long id1) {
				if (position % 2 == 1) {
					GridView g = (GridView) parent;
					Map<String, Object> listItemvalue = (Map<String, Object>) g
							.getItemAtPosition(position-1);
					String UID = (String) listItemvalue.get("cell");
					new XiaoqunyangDao(AddFeeder.this).deleteForUID(UID);
					listItems.remove(position);
					listItems.remove(position - 1);
				}
				simpleAdapter.notifyDataSetChanged();

			}
		});
	}

	// 表格加载数据
	public void setListItems(List<Map<String, Object>> listItems) {
		ArrayList<XiaoQunYnag> list = new XiaoqunyangDao(AddFeeder.this).selectAll();
		ArrayList<byte[]> blist = new XiaoqunyangDao(AddFeeder.this).select();
		for (int i = 0; i < list.size(); i++) {
			Map<String, Object> listItemvalue = new HashMap<String, Object>();
			listItemvalue.put("cell", "" + list.get(i).getUid());
			listItems.add(listItemvalue);

			Map<String, Object> listItemvalue1 = new HashMap<String, Object>();
			listItemvalue1.put("cell", "删除");
			listItems.add(listItemvalue1);

		}
	}

	// 表格适配器
	class GridAdapter extends SimpleAdapter {
		Context m_context;

		public GridAdapter(Context context,
						   List<? extends Map<String, ?>> data, int resource,
						   String[] from, int[] to) {
			super(context, data, resource, from, to);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			View lView = super.getView(position, convertView, parent);

			lView.setBackgroundColor(Color.WHITE);

			if (position / 2 % 2 == 1) {
				lView.setBackgroundColor(Color.rgb(0xdd, 0xdd, 0xdd));
			}
			return lView;
		}

	}

	// 二维码返回值
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		// 处理扫描结果（在界面上显示）
		if (resultCode == RESULT_OK) {
			if (requestCode == 1) {

				Bundle bundle = data.getExtras();
				String scanResult = bundle.getString("result");
				String[] ss = scanResult.split("\"");
				if(!(ss[1].equals("appkey")&&ss[5].equals("uid")))return;
				scanResult = ss[ss.length - 2];
				Toast.makeText(this, scanResult, Toast.LENGTH_LONG).show();
//				Register.getRegister().server.dingyue(scanResult + "&TX");
//
//				Register.getRegister().server.uID.put(scanResult + "&TX",
//						new ArrayList<byte[]>());
//				//
//				// new AddFeeder().findInfo(scanResult);
//
//				if(!new XiaoqunyangDao().panduanUID(scanResult)){
//					new XiaoqunyangDao().insert(scanResult);
//				}else{
//					Toast.makeText(this, "此设备已存在！！", Toast.LENGTH_LONG).show();
//				}


			}
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		for (int i = listItems.size()-1; -1 < i; i--) {
			listItems.remove(i);
		}
		setListItems(listItems);
		simpleAdapter.notifyDataSetChanged();
	}

}
